const app = getApp()
Page({

	data: {
		movies: [],
		_type: ''
	},

	onLoad: function (options) {
		this.data._type = options.type
		console.log(this.data._type)
		wx.request({
			url: app.gBaseUrl + this.data._type,
			data: {
				start: 0,
				count: 12
			},
			success: (res) => {
				this.setData({
					movies: res.data.subjects
				})
			}
		})
	},
	onReady() {
		let title = ''
		switch (this.data._type) {
			case 'in_theaters':
				title = '正在热映'
				break;
			case 'coming_soon':
				title = '即将上映'
				break;
			case 'top250':
				title = '豆瓣Top250'
				break;
			default:
				break;
		}
		wx.setNavigationBarTitle({
			title:title
		})
	},
	//上拉触底事件
	onReachBottom() {
		wx.showNavigationBarLoading()
		wx.request({
			url: app.gBaseUrl + this.data._type,
			data: {
				start: this.data.movies.length,
				count: 12
			},
			success: (res) => {
				this.setData({
					movies: this.data.movies.concat(res.data.subjects)
				})
				wx.hideNavigationBarLoading()
			}
		})
	},
	onPullDownRefresh() {
		wx.request({
			url: app.gBaseUrl + this.data._type,
			data: {
				start: 0,
				count: 12
			},
			success: (res) => {
				this.setData({
					movies: res.data.subjects
				})
				wx.stopPullDownRefresh()
			}
		})
	}
})